<?php

/**
 * Created by PhpStorm.
 * User: zhangw
 * Date: 2018/1/19
 * Time: 15:38
 * 
 * Modify: day
 * Time: 2018/8/9 9:48
 */

namespace App\Services\ThirdAPI;

use App\Models\Partner;
use App\Models\PartnerConfig;
use common\utils\CommonFun;
use common\utils\RSACrypt;
use App\Services\HallService;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Carbon;
use App\Models\Game;
use App\Models\GameTagRelation;
use common\utils\SysCode;
use common\enum\CommonEnum;

class GameApiService
{
    // 最新标签
    const GAME_TAG_NEW = 1;
    // 热门标签
    const GAME_TAG_HOT = 2;
    /**
     * 获取 API 到 partnerID 等信息
     */
    public static function getApiPartnerInfoByName($apiName)
    {
        return Partner::where(['name' => $apiName])->first();
    }

    /**
     * 获取厅某游戏的配置
     * @param $gamename
     * @param int $tid
     * @return array
     */
    public static function getTingGameConfig($gamename, $tid = 0)
    {
        if (empty($gamename)) {
            return [];
        }

        if ($tid == 0) {
            $tid = PartnerService::getDefaultGameConfig($gamename);
        }

        return PartnerService::getGameConfigByName($tid, $gamename);
    }

    /**
     * 第三方 api http 请求
     * @param string $url 请求地址
     * @param string $method 请求api的方法，get，post，patch 等
     * @param array $data 数据，body数据，param 数据等
     * @param array $header 头部信息
     * @param bool $formatJson 请求数据是否为json串
     * @return object
     */
    public static function request($url, $method, $data, $header = [], $formatJson = true)
    {
        $resp = request($url, $method, $data, $header, $formatJson);
        $ret = json_decode($resp->getBody()->getContents(), true);
        // 记录失败LOG
        if ($resp->getStatusCode() != 200 || !isset($ret['code']) || $ret['code'] != 0) {
            Log::error("RPC Request Error: ", ['url' => $url, 'method' => $method, 'data' => $data, 'status' => $resp->getStatusCode(), 'ret' => $ret]);
            error_exit($ret['code'] ?? 500, $ret['msg'] ?? "rpcserver API 请求出错");
        }
        if (!isset($ret['data'])) {
            Log::error("Request error:" . json_encode($ret));
            error_exit(SysCode::FAILED);
        }
        return $ret['data'];
    }

    /**
     * 电子游戏保存游戏
     */
    public static function saveGame($data)
    {
        // 电子，棋牌新增游戏默认增加热门，最新标签1个月
        foreach ($data as &$d) {
            if (in_array($d['game_type'], [CommonEnum::GAME_EGAME, CommonEnum::GAME_CARD])) {
                $d['hot_end_time'] = Carbon::now()->addMonth();
                $d['new_end_time'] = Carbon::now()->addMonth();
            }
        }
       return Game::insert($data);
    }

    /* ================================= day 分界线 ===============================*/

    /**
     * 计算体育有效投注
     * @param string $oddsStyle 赔率类型  MY：马来赔率，HK：香港赔率  ID：印尼赔率 US：美国赔率 EU：欧洲赔率
     * @param string $odds 赔率
     * @param string $betMoney 投注金额
     * @param string $result  输赢结果  1全赢 2全输 3赢半 4输半
     * @return string $validBet 有效投注金额
     */

    public static function calcValidBet($oddsStyle, $odds, $betMoney, $result)
    {
        switch ($oddsStyle) {
            case "MY":
                if ($odds <= 1 && $odds >= 0) {
                    if ($result == 1) {
                        $validBet = $betMoney * $odds;
                    } elseif ($result == 2) {
                        $validBet = $betMoney;
                    } elseif ($result == 3) {
                        $validBet = ($betMoney * $odds) / 2;
                    } elseif ($result == 4) {
                        $validBet = $betMoney / 2;
                    } else {
                        $validBet = 0;          //打和为0
                    }
                } elseif ($odds < 0) {
                    if ($result == 1 || $result == 2) {
                        $validBet = $betMoney * abs($odds);
                    } elseif ($result == 3 || $result == 4) {
                        $validBet = ($betMoney * abs($odds)) / 2;
                    } else {
                        $validBet = 0;       //打和为0
                    }
                } else {
                    $validBet = $betMoney;
                }
                break;
            case "HK":
                if ($odds >= 1) {
                    if ($result == 1 || $result == 2) {
                        $validBet = $betMoney;
                    } elseif ($result == 3 || $result == 4) {
                        $validBet = $betMoney / 2;
                    } else {
                        $validBet = 0;     //打和为0
                    }
                } else {
                    if ($result == 1) {
                        $validBet = $betMoney * $odds;
                    } elseif ($result == 2) {
                        $validBet = $betMoney;
                    } elseif ($result == 3) {
                        $validBet = ($betMoney * $odds) / 2;
                    } elseif ($result == 4) {
                        $validBet = $betMoney / 2;
                    } else {
                        $validBet = 0;        //打和为0
                    }
                }
                break;
            case "ID":
                if ($odds >= 1) {
                    if ($result == 1 || $result == 2) {
                        $validBet = $betMoney;
                    } elseif ($result == 3 || $result == 4) {
                        $validBet = $betMoney / 2;
                    } else {
                        $validBet = 0;      //打和为0
                    }
                } elseif ($odds <= -1) {
                    if ($result == 1) {
                        $validBet = $betMoney;
                    } elseif ($result == 2) {
                        $validBet = $betMoney * abs($odds);
                    } elseif ($result == 3) {
                        $validBet = $betMoney / 2;
                    } elseif ($result == 4) {
                        $validBet = ($betMoney * abs($odds)) / 2;
                    } else {
                        $validBet = 0;          //打和为0
                    }
                } else {
                    $validBet = $betMoney;
                }
                break;
            case "US":
                if ($odds >= 100) {
                    if ($result == 1 || $result == 2) {
                        $validBet = $betMoney;
                    } elseif ($result == 3 || $result == 4) {
                        $validBet = $betMoney / 2;
                    } else {
                        $validBet = 0;      //打和为0
                    }
                } elseif ($odds <= -100) {
                    if ($result == 1) {
                        $validBet = $betMoney;
                    } elseif ($result == 2) {
                        $validBet = $betMoney * abs($odds) / 100;
                    } elseif ($result == 3) {
                        $validBet = $betMoney / 2;
                    } elseif ($result == 4) {
                        $validBet = ($betMoney * abs($odds)) / 100 / 2;
                    } else {
                        $validBet = 0;      //打和为0
                    }
                } else {
                    $validBet = $betMoney;
                }
                break;
            case "EU":
                if ($odds >= 2) {
                    if ($result == 1 || $result == 2) {
                        $validBet = $betMoney;
                    } elseif ($result == 3 || $result == 4) {
                        $validBet = $betMoney / 2;
                    } else {
                        $validBet = 0;       //打和为0
                    }
                } else {
                    if ($result == 1) {
                        $validBet = $betMoney * ($odds - 1);
                    } elseif ($result == 2) {
                        $validBet = $betMoney;
                    } elseif ($result == 3) {
                        $validBet = ($betMoney * ($odds - 1)) / 2;
                    } elseif ($result == 4) {
                        $validBet = $betMoney / 2;
                    } else {
                        $validBet = 0;       //打和为0
                    }
                }
                break;
            default:
                $validBet = $betMoney;
        }
        return $validBet;
    }
}